package com.fulihui.wgj.admin.integration.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.fulihui.common.builder.web.PageViewBuilder;
import com.fulihui.common.conv.BeanConv;
import com.fulihui.common.result.rpc.SimpleResult;
import com.fulihui.common.result.web.PageView;
import com.fulihui.common.util.RpcResultCheckUtil;
import com.fulihui.wgj.admin.api.platform.request.WxPlatformPageReq;
import com.fulihui.wgj.admin.api.platform.vo.WxPlatformVO;
import com.fulihui.wgj.admin.config.properties.AdminProperties;
import com.fulihui.wgj.admin.integration.WxPlatformClient;
import com.fulihui.wgj.facade.dto.WxPlatformDTO;
import com.fulihui.wgj.facade.request.platform.WxPlatformLoginPageRequest;
import com.fulihui.wgj.facade.result.platform.WxPlatformPageResult;
import com.fulihui.wgj.facade.service.platform.WxPlatformService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: JY
 * @date: 2018/7/3 10:31
 */
@Service("wxPlatformServiceClient")
public class WxPlatformClientImpl implements WxPlatformClient {

    @Reference(version = "1.0.0", timeout = 20000)
    WxPlatformService wxPlatformService;

    @Autowired
    AdminProperties adminProperties;

    /**
     * 公众号列表
     *
     * @param req
     */
    @Override
    public PageView<WxPlatformVO> queryWxPlatformPage(WxPlatformPageReq req) {

        WxPlatformLoginPageRequest request = BeanConv.copy(req, WxPlatformLoginPageRequest.class);

        //调用rpc
        WxPlatformPageResult rpcResult = wxPlatformService.queryWxPlatformPageHasWxUser(request);

        List<WxPlatformVO> values = null;

        if (rpcResult.isSuccess()) {
            //获取公众号列表
            List<WxPlatformDTO> listPlatform = rpcResult.getValue();

            if (listPlatform != null && listPlatform.size() > 0) {
                values = listPlatform.stream().map((item) -> {
                    //对象转换
                    WxPlatformVO wxPlatformVO = BeanConv.copy(item, WxPlatformVO.class);
                    //粉丝总量数据
                    if (item.getWxUserCumulateDTO() != null) {
                        wxPlatformVO.setCumulateUser(item.getWxUserCumulateDTO().getCumulateUser());
                    }
                    //粉丝增加数据
                    if (item.getWxUserSummaryDTO() != null) {
                        wxPlatformVO.setNewUser(item.getWxUserSummaryDTO().getNewUser());
                        wxPlatformVO.setCanceUuser(item.getWxUserSummaryDTO().getCanceUuser());
                        wxPlatformVO.setNetGrowth(item.getWxUserSummaryDTO().getNetGrowth());
                    }
                    //订阅号 服务号
                    wxPlatformVO.setServiceTypeInfo("2".equals(item.getServiceTypeInfo()) ? "2" : "1");
                    wxPlatformVO.setQrcodeUrl(adminProperties.getImgproxy() + item.getQrcodeUrl());
                    return wxPlatformVO;
                }).collect(Collectors.toList());
            }
        }
        return PageViewBuilder.build(values, rpcResult.getPage(),
                rpcResult.getRows(), rpcResult.getTotalRows());
    }

    /**
     * 获取token
     *
     * @param appId
     * @return
     */
    @Override
    public String queryTokenFromRedis(String appId) {
        SimpleResult<String> simpleResult = wxPlatformService.queryTokenFromRedis(appId);
        if (RpcResultCheckUtil.isSuccess(simpleResult)) {
            return simpleResult.getData();
        }
        return null;
    }
}
